package com.example.gokchinesefoodmapmcdev.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.gokchinesefoodmapmcdev.entity.MarketingPicture;
import com.example.gokchinesefoodmapmcdev.service.GoodsService;
import com.example.gokchinesefoodmapmcdev.util.ResultData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest")
public class HomepageController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 首页推荐商品信息查询
     */
    @PostMapping("/app/good/recommendInfo")
    public ResultData recommendInfo(@RequestBody Map<String, Object> map){
        Integer pageNumber = (Integer)map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        ResultData resultData = goodsService.findRecommendGoodsInfo(pageNumber, pageSize);
        return resultData;
    }

    /**
     * 首页图片列表查询信息
     * 练习提交
     */
    @PostMapping("/marketing/marketingPicture/list")
    public ResultData marketingPictureList(){
        List<MarketingPicture> marketingPicture = goodsService.findMarketingPicture();
        ResultData resultData = new ResultData();
        resultData.put("code", ResultData.OPERATION_SUCCESS);
        resultData.put("rows", marketingPicture);
        return resultData;
    }

    /**
     * 客户端查看商品详情
     * @param map 商品Id
     * @return
     */
    @PostMapping("/app/good/info")
    public ResultData goodDetailInfo(@RequestBody Map<String, Object> map){
        Integer goodId = (Integer) map.get("id");
        ResultData resultData = goodsService.findGoodInfoByGoodId(goodId);
        return resultData;
    }

    /**
     * 从商品详情页确认订单
     *  goodId    商品id
     *  symbol    商品规格号
     *  number    商品数量
     * @return
     */
    @PostMapping("/app/order/confirmOrder")
    public ResultData confirmOrder(@RequestBody Map<String, Object> map){
        Integer goodId = (Integer) map.get("goodId");
        List symbolList = (List)map.get("symbol");
        Integer[] symbol = new Integer[symbolList.size()];
        for(int i = 0; i < symbol.length; i++){
            symbol[i] = (Integer) symbolList.get(i);
        }
        Integer number = (Integer) map.get("number");
        ResultData resultData = goodsService.confirmOrder(goodId, symbol, number);
        return resultData;
    }

}
